{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Category Plots, aka Bar Charts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2, 3], [1, 3, 5]])\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cplot = new CategoryPlot(initWidth: 400, initHeight: 200)\n",
    "def bars = new CategoryBars(value:[[1, 2, 3], [1, 3, 5]])\n",
    "cplot << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cplot = new CategoryPlot(title: \"Hello CategoryPlot!\",\n",
    "                             xLabel: \"Categories\",\n",
    "                             yLabel: \"Values\")\n",
    "cplot << new CategoryBars(value:[[1, 2, 3], [1, 3, 5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cplot = new CategoryPlot(categoryNames: [\"Helium\", \"Neon\", \"Argon\"])\n",
    "cplot << new CategoryBars(value: [[1, 2, 3], [1, 3, 5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new CategoryPlot() << new CategoryBars(value: [[1, 2, 3], [1, 3, 5]],\n",
    "                                       seriesNames: [\"Gas\", \"Liquid\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2], [3, 4], [5, 6], [7, 8]],\n",
    "                            seriesNames: [\"Gas\", null, \"\", \"Liquid\"])\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot = new CategoryPlot(showLegend: true) // force legend display\n",
    "def bars = new CategoryBars(value: [[1, 2, 3], [1, 3, 5]])\n",
    "// since no display names were provided, default names \"series0\" etc will be used.\n",
    "plot << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot = new CategoryPlot(orientation: PlotOrientationType.HORIZONTAL)\n",
    "plot << new CategoryBars(value:[[1, 2, 3], [1, 3, 5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot = new CategoryPlot(categoryNames: [\"Acid\", \"Neutral\", \"Base\"], \n",
    "                            categoryNamesLabelAngle: -1/4 * Math.PI)\n",
    "plot << new CategoryBars(value: [[1, 2, 3], [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new CategoryPlot(categoryMargin: 2) << new CategoryBars(value: [[1, 2, 3], [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: (1..4) + 2)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2], [3, 4], [5, 6]], color: Color.pink)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def colors = [Color.red, Color.gray, Color.blue]\n",
    "def bars = new CategoryBars(value: [[1, 2], [3, 4], [5, 6]], color: colors)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def colors = [[Color.red, Color.gray],\n",
    "              [Color.gray, Color.gray],\n",
    "              [Color.blue, Color.pink]]\n",
    "def bars = new CategoryBars(value: [[1, 2], [3, 4], [5, 6]], color: colors)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def colors = [Color.pink, [Color.red, Color.gray, Color.blue]]\n",
    "def bars = new CategoryBars(value: [[1, 2, 3], [4, 5, 6]], color: colors)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2, 3], [4, 5, 6]], base: -2)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2,  3], [4, 5, 4]], base: [-1, -3])\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2, 3], [4, 5, 6]],\n",
    "                            base: [[0, 1, 1], [-4, -5, -6]])\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2, 3], [4, 5, 6]],\n",
    "                            width: [[0.3, 0.6, 1.7], 1.0])\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2, 3], [4, 5, 6]],\n",
    "                            fill: [[true, true, false], [true, false, true]],\n",
    "                            drawOutline: [[true, false, true], [true, true, false]],\n",
    "                            outlineColor: [Color.black, Color.red])\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[1, 2, 3], [4, 5, 8], [10, 9, 10]],\n",
    "                           base: [0, [1, 2, 3], [4, 5, 8]],\n",
    "                           centerSeries: true,\n",
    "                           itemLabel: {value, base-> Math.abs(value-base)})\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ss = [StrokeType.DASH, StrokeType.LONGDASH]\n",
    "def cs = [Color.black, Color.red]\n",
    "new CategoryPlot() << new CategoryStems(value: [[1, 2, 4], [4, 5, 8]], color: cs, style: ss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new CategoryPlot() << new CategoryPoints(value: [[1, 2, 4], [4, 5, 8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ss = [StrokeType.DASH, StrokeType.DOT]\n",
    "new CategoryPlot() << new CategoryLines(value: [[1, 2, 4], [4, 5, 8]], style: ss,\n",
    "                                        seriesNames:[\"Lanthanide\", \"Actinide\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def s1 = [1, 2, 4]\n",
    "def s2 = [4, 5, 8]\n",
    "def lines = new CategoryLines(value: [s1, s2], centerSeries: true)\n",
    "def points = new CategoryPoints(value: [s1, s2], centerSeries: true)\n",
    "def stems = new CategoryStems(value:[s1], base: [s2], style: StrokeType.DOT, color: Color.gray)\n",
    "new CategoryPlot() << lines << points << stems"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot = new CategoryPlot(initWidth: 500, initHeight: 400,\n",
    "                            title: \"Bar Chart Demo\",\n",
    "                            xLabel: \"Alkali\", yLabel: \"Temperature ° Celcius\",\n",
    "                            categoryNames:[\"Lithium\", \"Sodium\", \"Potassium\", \"Rubidium\"])\n",
    "def s1 = [[10, 15, 13, 7], [22, 18, 28, 17]]\n",
    "def high = [[12.4, 19.5, 15.1, 8.2], [24.3, 23.3, 30.1, 18.2]]\n",
    "def low = [[7.6, 10.5, 10.9, 5.8], [19.7, 12.7, 25.9, 15.8]]\n",
    "def color = [new Color(247, 150, 70), Color.orange, new Color(155, 187, 89)]\n",
    "plot << new CategoryBars(value: s1, color: color, seriesNames: [\"Solid\", \"Liquid\"])\n",
    "plot << new CategoryStems(value: high, base: low, color: color[2])\n",
    "plot << new CategoryPoints(value: high, outlineColor: color[2], size:12)\n",
    "plot << new CategoryPoints(value: low, outlineColor: color[2], size:12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def p = new CategoryPlot(title: \"Multiple Y Axes Demo\",\n",
    "                         yLabel: \"Price\",\n",
    "                         categoryNames: [\"Q1\", \"Q2\", \"Q3\", \"Q4\"])\n",
    "p << new YAxis(label: \"Volume\", upperMargin: 1)\n",
    "p << new CategoryBars(value: [[1500, 2200, 2500, 4000]], width: 0.6,\n",
    "                      color: Color.PINK, yAxis: \"Volume\", showItemLabel: true,\n",
    "                      labelPosition: LabelPositionType.VALUE_INSIDE)\n",
    "p << new CategoryLines(value: [[5, 2, 3.5, 4]], color: Color.GRAY,\n",
    "                       showItemLabel:true)\n",
    "p << new CategoryPoints(value:[[5, 2, 3.5, 4]], color:Color.GRAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new CategoryPlot() << new CategoryStems(value:[[-3, 2, 4], [4, 5, 8]],\n",
    "                                        width: 10, showItemLabel: true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[-5, 2, 3], [1, 3, 5]],\n",
    "                            showItemLabel: true)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bars = new CategoryBars(value: [[-5, 2, 3], [1, 3, 5]],\n",
    "                            labelPosition: LabelPositionType.BASE_OUTSIDE,\n",
    "                            showItemLabel: true)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot = new CategoryPlot(title: \"Move mouse cursor over bars\")\n",
    "def bars = new CategoryBars(value: [[-5, 2, 3], [1, 3, 5]], useToolTip: false)\n",
    "plot << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def input = [[1,2,3],[1,3,5]].transpose() // == [[1,1], [2,3], [3,5]]\n",
    "def bars = new CategoryBars(value: input)\n",
    "new CategoryPlot() << bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "table = [[close:11.59, high:13.15, low:11.92, open:11.92],\n",
    "         [close:12.76, high:15.44, low:11.88, open:12.42],\n",
    "         [close:18.19, high:20.96, low:17.93, open:18.56]]\n",
    "def v = table.collect { it.values().toList() }\n",
    "def p = new CategoryPlot(categoryNames: table[0].keySet().toList())\n",
    "p << new CategoryBars(value: v, seriesNames: [\"A\", \"B\", \"C\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cs = [Color.orange]\n",
    "\n",
    "def cp = new CategoryPlot() \n",
    "\n",
    "cp << new CategoryArea(value: [[1, 3, 2]], base: [[0.5, 1, 0]])\n",
    "cp << new CategoryArea(value: [[2, 1, 0.5]],  color: cs)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "classpath": [],
  "imports": [],
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
